<?php

/**
 * test case for uTpl class
 */
class TestUtpl extends UnitTestCase {

    function testNoVars() {
        $str='This is a no vars template';
        $this->_testRendering($str,array(),$str);
    }

    function testPureVars() {
        // one var in the middle
        $tpl=new uTpl('Hello {{who}} !');
        $this->assertEqual($tpl->render(array('who'=>'World')),'Hello World !');
        $this->assertEqual($tpl->render(array('who'=>'to the red Moon')),'Hello to the red Moon !');
        $this->assertEqual($tpl->render(array('who'=>null)),'Hello  !');

        // two vars at the borders
        $tpl=new uTpl('{{start}} some text {{end}}');
        $this->assertEqual(
            $tpl->render(array('start'=>'ceci est un debut','end'=>'ceci est la fin'))
            ,'ceci est un debut some text ceci est la fin');

        // repeating var, complex name
        $tpl=new uTpl('one {{z_r0}} two {{z_r0}} three');
        $this->assertEqual(
            $tpl->render(array('z_r0'=>'Zero'))
            ,'one Zero two Zero three');
    }

    function testTypes() {
        // integer
        $tpl=new uTpl('number is {{number}}');
        $this->assertEqual($tpl->render(array('number'=>36)),'number is 36');
    }

    function testRealCases() {
        $tpl=new uTpl("corbeille='0'
	{AND annee={anneeref}}");
        $result="corbeille='0'
	AND annee=2008";
        $this->assertEqual($tpl->render(array('anneeref'=>2008)),$result);

    }

    function testConditional() {
        $tpl=new uTpl('Hello{ nice {who} !}');
        $this->assertEqual(
            $tpl->render(array('who'=>null))
            ,'Hello');
        $this->assertEqual(
            $tpl->render(array('who'=>'people'))
            ,'Hello nice people !');

        // booleans
        $tpl=new uTpl('start {this {bool} is true} end');
        $this->assertEqual(
            $tpl->render(array('bool'=>true))
            ,'start this  is true end');
        $this->assertEqual(
            $tpl->render(array('bool'=>false))
            ,'start  end');

    }

    /**
     * test rendering
     * @param string $str source template
     * @param array $data vars to inject
     * @param string $result expected result
     */
    function _testRendering($str,$data,$result) {
        $tpl=new uTpl($str);
        $this->assertEqual($tpl->render($data),$result);
    }
}
